print("------------ extension扩展属性 ------------")
// 跟oc的category有点类似
extension Double {
    var km: Double { return self * 1_000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0  }
}

let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")

print("------------ extension扩展便捷初始化器 ------------")

print("------------ extension扩展方法 ------------")
extension Int {
    func repeatStart(task: () -> Void) {
        for _ in 0..<3 {
            task()
        }
    }
}
3.repeatStart {
    print("hi")
}

print("------------ extension扩展mutating方法 ------------")
extension Int {
    mutating func square() {
        self = self * self
    }
}
var a = 3
a.square()
print(a)

print("------------ extension扩展下标 ------------")
extension Int {
    subscript(digitIndex: Int) -> Int {
        var decimalBase = 1
        for _ in 0..<digitIndex {
            decimalBase *= 10
        }
        return (self / decimalBase) % 10
    }
}
print(31415926[0]) // 个位数
print(31415926[1]) // 十位数
print(31415926[2]) // 百位数
print(31415926[3]) // 千位数

print("------------ extension扩展内嵌类型 ------------")
extension Int {
    enum kind {
        case negative, zero, positive
    }
    var kind: kind {
        switch self {
        case 0:
            return .zero
        case let x where x > 0:
            return .positive
        default:
            return .negative
        }
    }
}

let num = 12
print(num.kind)
print(type(of: num.kind))

